package com.zut.chat.controller;

import com.zut.chat.entity.ChatBotConfig;
import com.zut.chat.service.ChatBotConfigService;
import com.zut.chat.service.ChatBotAutoReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 聊天机器人配置管理控制器
 */
@RestController
@RequestMapping("/api/chatbot")
@CrossOrigin(origins = "*")
public class ChatBotController {

    @Autowired
    private ChatBotConfigService chatBotConfigService;

    @Autowired
    private ChatBotAutoReplyService chatBotAutoReplyService;

    /**
     * 获取所有机器人配置
     */
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getAllBots() {
        try {
            List<ChatBotConfig> bots = chatBotConfigService.list();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", bots);
            response.put("message", "获取机器人配置成功");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "获取机器人配置失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 获取启用的机器人配置
     */
    @GetMapping("/enabled")
    public ResponseEntity<Map<String, Object>> getEnabledBots() {
        try {
            List<ChatBotConfig> bots = chatBotConfigService.getEnabledBots();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("data", bots);
            response.put("message", "获取启用机器人配置成功");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "获取启用机器人配置失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 创建新的机器人配置
     */
    @PostMapping("/create")
    public ResponseEntity<Map<String, Object>> createBot(@RequestBody ChatBotConfig config) {
        try {
            boolean success = chatBotConfigService.save(config);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", success);
            response.put("message", success ? "创建机器人配置成功" : "创建机器人配置失败");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "创建机器人配置失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 更新机器人配置
     */
    @PutMapping("/update/{id}")
    public ResponseEntity<Map<String, Object>> updateBot(@PathVariable Long id, @RequestBody ChatBotConfig config) {
        try {
            config.setId(id);
            boolean success = chatBotConfigService.updateBotConfig(config);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", success);
            response.put("message", success ? "更新机器人配置成功" : "更新机器人配置失败");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "更新机器人配置失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 删除机器人配置
     */
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Map<String, Object>> deleteBot(@PathVariable Long id) {
        try {
            boolean success = chatBotConfigService.deleteBotConfig(id);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", success);
            response.put("message", success ? "删除机器人配置成功" : "删除机器人配置失败");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "删除机器人配置失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 启用或禁用机器人
     */
    @PostMapping("/toggle-enabled/{id}")
    public ResponseEntity<Map<String, Object>> toggleBotEnabled(@PathVariable Long id, @RequestParam boolean enabled) {
        try {
            boolean success = chatBotConfigService.toggleBotEnabled(id, enabled);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", success);
            response.put("message", success ? (enabled ? "启用机器人成功" : "禁用机器人成功") : "操作失败");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "操作失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 启用或禁用机器人自动回复
     */
    @PostMapping("/toggle-auto-reply/{id}")
    public ResponseEntity<Map<String, Object>> toggleAutoReply(@PathVariable Long id, @RequestParam boolean autoReplyEnabled) {
        try {
            boolean success = chatBotConfigService.toggleAutoReply(id, autoReplyEnabled);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", success);
            response.put("message", success ? (autoReplyEnabled ? "启用自动回复成功" : "禁用自动回复成功") : "操作失败");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "操作失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 手动触发机器人回复（用于测试）
     */
    @PostMapping("/manual-reply")
    public ResponseEntity<Map<String, Object>> manualReply(@RequestParam String botName, @RequestParam String content) {
        try {
            chatBotAutoReplyService.triggerManualReply(botName, content);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "手动触发机器人回复成功");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "手动触发机器人回复失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 重置机器人回复时间
     */
    @PostMapping("/reset-reply-time/{id}")
    public ResponseEntity<Map<String, Object>> resetBotReplyTime(@PathVariable Long id) {
        try {
            chatBotAutoReplyService.resetBotReplyTime(id);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "重置机器人回复时间成功");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "重置机器人回复时间失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }

    /**
     * 测试机器人视频功能
     */
    @PostMapping("/test-video")
    public ResponseEntity<Map<String, Object>> testBotVideo(@RequestParam String botName) {
        try {
            chatBotAutoReplyService.handleVideoRequest(null);
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "机器人视频测试成功");
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "机器人视频测试失败：" + e.getMessage());
            return ResponseEntity.ok(response);
        }
    }
} 